package com.lzy.canal.component.filter.mysql;

import com.lzy.canal.config.CanalClientProperty;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mysql 过滤器配置方式实现
 *
 * @author lzy
 */

public class MySqlByConfigFilter implements MySqlFilter {
    private CanalClientProperty property;

    @Override
    public boolean apply(String tableName, String schemaName) {
        ConcurrentHashMap<String, HashSet<String>> schemaTables = property.getMysqlSchemaTables();
        if (CollectionUtils.isEmpty(schemaTables)){
            return false;
        }
        return Optional.ofNullable(schemaTables.get(schemaName)).orElse(new HashSet<>()).contains(tableName);
    }

    public MySqlByConfigFilter(CanalClientProperty property) {
        this.property = property;
    }
}
